package com.hskn.hss.module.permissionRole.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.othercarcsotCfg.mapper.TlOthercarcostCfgMapper;
import com.hskn.hss.module.permissionRole.entity.SysPermissionRole;
import com.hskn.hss.module.permissionRole.mapper.SysPermissionRoleMapper;
import com.hskn.hss.module.permissionRole.service.ISysPermissionRoleService;
import com.hskn.hss.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-04-30
 */
@Slf4j
@RestController
@RequestMapping("/hss/syspermissionrole")
public class SysPermissionRoleController {
    @Autowired
    private ISysPermissionRoleService sysPermissionRoleService;

    /**
     * 新增
     *
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.INSERT, tableName = "sys_permission_role", bean = SysPermissionRole.class, beanMapper = SysPermissionRoleMapper.class, names = "roleid, permissionid")
    @RequestMapping(value = "insertPermissionRole", method = RequestMethod.POST)
    public AjaxResult<Object> insertPermissionRole(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysPermissionRoleService.insert(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "sys_permission_role", idKey = "id", bean = SysPermissionRole.class, beanMapper = SysPermissionRoleMapper.class, names = "roleid, permissionid")
    @RequestMapping(value = "updatePermissionRole", method = RequestMethod.POST)
    public AjaxResult<Object> updatePermissionRole(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysPermissionRoleService.update(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "sys_permission_role", idKey = "id", bean = SysPermissionRole.class, beanMapper = SysPermissionRoleMapper.class, names = "roleid, permissionid")
    @RequestMapping(value = "updatePermissionRoleByUserId", method = RequestMethod.POST)
    public AjaxResult<Object> updatePermissionRoleByUserId(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysPermissionRoleService.update(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "sys_permission_role", idKey = "id", bean = SysPermissionRole.class, beanMapper = SysPermissionRoleMapper.class, names = "roleid, permissionid")
    @RequestMapping(value = "deletePermissionRoleById", method = RequestMethod.POST)
    public AjaxResult<Object> deletePermissionRoleById(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            return sysPermissionRoleService.deleteByid(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("删除失败，请稍后再试");
        }
    }

    @RequestMapping(value = "getPermissionRoleById", method = RequestMethod.POST)
    public AjaxResult<Object> getPermissionRoleById(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        SysPermissionRole sysPermissionRole = null;
        try {
            sysPermissionRole = sysPermissionRoleService.getInfoById(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
        return AjaxResult.success(sysPermissionRole);

    }

    @RequestMapping(value = "getPermissionRoleList", method = RequestMethod.POST)
    public AjaxResult<Object> getPermissionRoleList(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        List<SysPermissionRole> sysPermissionRoleList = null;
        try {
            sysPermissionRoleList = sysPermissionRoleService.getListByMap(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
        return AjaxResult.success(sysPermissionRoleList);
    }
}

